package ru.papergames.web.blines.battle.network.clientpackets;

import javolution.util.FastMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.papergames.common.beans.BattleMember;
import ru.papergames.web.blines.battle.model.BattleOrder2;

/**
 * @author Alexander Troshanin
 */
public class CreateNewBattle extends WebServerClientPacket {

    private static Log log = LogFactory.getLog(CreateNewBattle.class);

    private BattleOrder2 battleOrder;

    public CreateNewBattle(BattleOrder2 battleOrder) {
        this.battleOrder = battleOrder;
    }

    @Override
    protected void writePacketId() {
        writeC(CREATE_NEW_BATTLE_PACKET_ID);
    }

    @Override
    protected void writePacketData() {
        writeD(battleOrder.getId());
        writeC(battleOrder.getTeamsCount());
        for (FastMap<Long, BattleMember> team : battleOrder.getTeams()) {
            writeC(team.size());
            for (FastMap.Entry<Long, BattleMember> e = team.head(), end = team.tail(); (e = e.getNext()) != end;) {
                writeQ(e.getValue().getId());
            }
        }
    }

    @Override
    public String getType() {
        return "[c] CreateNewBattle[" + CREATE_NEW_BATTLE_PACKET_ID + "]";
    }
}
